package com.doit.demo.day05;

import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

/**
 * @DATE 2022/2/19/10:05
 * @Author MDK
 * @Version 2021.2.2
 *
 * 所有的window,按照是否key后再进行划分窗口,分为keyedWindow和NonKeyedWindow
 *      如果没有KeyBy就划分窗口,就是NonKeyedWindow,
 *      底层调用的就是windowAll方法,window和window operator对应的Task并行度永远为1
 *
 **/
public class CountWindowAll {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> lines = env.socketTextStream("linux01", 8888);
        SingleOutputStreamOperator<Integer> nums = lines.map(Integer::parseInt);

        //没有keyBy,调用countWindowAll,按照条数划分窗口,当窗口中的数据条数达到一定数量后再进行输出
        AllWindowedStream<Integer, GlobalWindow> windowedStream = nums.countWindowAll(5);
        //划分窗口后,需要调用相应的方法
        SingleOutputStreamOperator<Integer> operator = windowedStream.sum(0);
        operator.print();
        env.execute();
    }
}
